﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Tour_ArrivedDestinationMan : System.Web.UI.UserControl
{
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        Alert1.HideMessage();
        this.initDataPaging();
        if (!IsPostBack)
        {
            ShowMess();
            BindDataToLanguageDropDown();
            BindDataToGrid(1);

        }
    }
    #region Paging & Message
    private void initDataPaging()
    {
        Pager1.PageSize = Config.PAGESIZE;
        Pager1.ShowFirstLast = true;
        Pager1.FirstClause = "Đầu";
        Pager1.LastClause = "Cuối";
        Pager1.NextClause = "Tiếp";
        Pager1.OfClause = "của";
        Pager1.PageClause = "Trang";
        Pager1.CompactedPageCount = 5;
        Pager1.PreviousClause = "Sau";
        Pager1.MaxSmartShortCutCount = 5;
        Pager1.NotCompactedPageCount = 5;
    }
    public void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndx;
        BindDataToGrid(currnetPageIndx);
    }
    private void VisiblePaging(int intPagecount)
    {
        int nPageSize = 0;
        nPageSize = Config.PAGESIZE;
        if (intPagecount > nPageSize)
            Pager1.Visible = true;
        else
            Pager1.Visible = false;
    }
    private void ShowMess()
    {
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        lblWarning.Text = MessagesAlert.SEARCH_NO_ITEM[nDefaultLanguage-1];
        switch (nMessage)
        {
            case 1:
                Alert1.ShowMessage(1, MessagesAlert.INSERT_SUCCESSFULL[nDefaultLanguage-1]);
                break;
            case 2:
                Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage-1]);
                break;
            default:
                Alert1.HideMessage();
                break;
        }
    }
    #endregion

    #region Click Events
    protected void btnAddnew_Click1(object sender, EventArgs e)
    {
        string sAddNew = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_ARRIVEDDESTINATION_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAddNew);
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string sAddNew = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TOUR + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAddNew);
    }
    #endregion
    #region BindDataToDDL
    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataToGrid(1);
    }
    #endregion
    #region BindDataToGrid
    private void BindDataToGrid(int CurrentPageIndex)
    {
        Transports objTransports = new Transports();
        DataTable dtbTransport = new DataTable();
        dtbTransport = objTransports.GetALL();
        dtgTransports.DataSource = dtbTransport;
        dtgTransports.DataBind();
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage - 1] + dtbTransport.Rows.Count + " " + MultipleLanguage.LBL_FOUND[nDefaultLanguage - 1];
        Pager1.ItemCount = dtbTransport.Rows.Count;

        VisiblePaging(dtbTransport.Rows.Count);
        if (dtbTransport.Rows.Count > 0)
            tblWarning.Visible = false;
        else
            tblWarning.Visible = true;
    }
    protected void dtgTransports_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        Transports objTransports = new Transports();
        switch (strCommand)
        {
            case "Delete":
                int nDelete = objTransports.DeleteById(nID);
                if (nDelete > 0)
                    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[nDefaultLanguage -1]);
                else
                    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[nDefaultLanguage-1]);
                BindDataToGrid(Pager1.CurrentIndex);
                break;

            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_TRANSPORT_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;
        }
    }
    protected void dtgTransports_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        int nCurrentLanguage = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);

        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblTour = (Label)e.Item.FindControl("lblTour");
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");


            switch (nCurrentLanguage)
            {
                case 1:
                    lblTour.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TransportName1"));
                    break;
                case 2:
                    lblTour.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TransportName2"));
                    break;
                case 3:
                    lblTour.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TransportName3"));
                    break;
            }

            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TransportID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nDefaultLanguage - 1]);
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "TransportID"));
        }
    }
    #endregion
}